package com.mz.mattress.client.service.impl;

import com.mz.mattress.api.common.Result;
import com.mz.mattress.api.entity.Merchant;
import com.mz.mattress.api.entity.MerchantDetail;
import com.mz.mattress.api.jwt.ClientJwt;
import com.mz.mattress.api.util.StringTools;
import com.mz.mattress.client.common.Holder;
import com.mz.mattress.client.mapper.MerchantDetailMapper;
import com.mz.mattress.client.mapper.MerchantMapper;
import com.mz.mattress.client.service.MerchantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@Slf4j
public class MerchantServiceImpl implements MerchantService {

    @Autowired
    private MerchantMapper merchantMapper;

    @Autowired
    private MerchantDetailMapper merchantDetailMapper;

    @Override
    public Result<Merchant> getById(Integer id) {
        Merchant merchant = merchantMapper.selectById(id);
        return Result.ok(merchant);
    }

    @Override
    public Result updateMobile(String mobile) {

        if (StringUtils.isEmpty(mobile) || mobile.length() != 11) {
            return Result.fail("请输入正确的手机号");
        }

        ClientJwt clientJwt = Holder.CLIENT_JWT_THREAD_LOCAL.get();
        Integer merchantId = clientJwt.getMerchantId();

//        if (isMaster && merchantId != null) {
//            Integer count = merchantMapper.selectCount(new LambdaQueryWrapper<Merchant>().eq(Merchant::getMobile, mobile));
//            if (count > 0) {
//                return Result.fail("该手机号已存在，不能使用");
//            }
//
//            Merchant update = new Merchant();
//            update.setId(merchantId);
//            update.setMobile(mobile);
//
//            merchantMapper.updateById(update);
//            log.info("update_merchant_mobile,Id={},mobile={}", merchantId, mobile);
//
//            return Result.ok();
//        }

        return Result.fail("没有权限");
    }

    @Override
    public Result updateImg() {

        List<MerchantDetail> list = merchantDetailMapper.list();

        for (MerchantDetail detail : list) {

            if (StringTools.isNotEmpty(detail.getShop())) {

                String shop = detail.getShop();
                String[] split = shop.split(",");

                if (split.length > 0) {

                    Merchant update = new Merchant();
                    update.setId(detail.getId());
                    update.setImg(split[0]);
                    merchantMapper.updateById(update);

                }

            }

        }


        return null;
    }

}
